home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 011 / ileave.arc / TYPE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-07  |  4.0 KB  |  173 lines

  1. #include <stdio.h>
  2. #include <hamdefs.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <dir.h>    
  7.  
  8. char    line[132];
  9. char    **lines;
  10. int    linecount, width, height;
  11. FILE *ptr;
  12.  
  13. type(filename, top, left, bottom, right)
  14. char    *filename;
  15. int    top, left, bottom, right;
  16. {
  17.     /*  this routine displays a file in the lower window of the
  18.     screen, and allows the user to use the cursor keys to
  19.     view more and more of the file.  standard text files
  20.     are used, which can also be printed and used by other
  21.     DOS functions
  22. */
  23.     int    cur_top, i, j;
  24.  
  25.     char    fname[80];
  26.  
  27.     strcpy(fname, searchpath(filename));
  28.  
  29.     lines = calloc(100, sizeof(char *));
  30.     csrpush();
  31.     width = right - left - 1;
  32.     height = bottom - top - 1;
  33.     linecount = 0;
  34.  
  35.     clrblk(top, left, bottom, right);
  36.     scrbox(top, left, bottom, right, 3, NORMAL | HILITE);
  37.     atputsha(top, left + (width / 2) - 6, "Press \030 or \031");
  38.     atputsha(bottom, left + (width / 2) - 6, "Esc when done");
  39.  
  40.     ptr = fopen(filename, "r");
  41.     if (ptr == NULL) {
  42.         clrblk(top, left, bottom, right);
  43.         csrpop();
  44.         free(lines);
  45.         return - 1;
  46.     }
  47.  
  48.     fillarray();
  49.  
  50.     cursor(FALSE);
  51.     for (i = 0; i < linecount && i < height; i++)
  52.         atputsha(top + 1 + i, left + 1, lines[i]);
  53.  
  54.     cur_top = 0;
  55.  
  56.     for (; ; ) {
  57.         switch (j = inkeyi()) {
  58.         case HOME:    
  59.             cur_top = 0;
  60.             clrblk(top + 1, left + 1, bottom - 1, right - 1);
  61.             for (i = 0; i < linecount && i < height; i++)
  62.                 atputsha(top + 1 + i, left + 1, lines[i]);
  63.             break;
  64.  
  65.         case END:    
  66.             cur_top = linecount - height;
  67.             clrblk(top + 1, left + 1, bottom - 1, right - 1);
  68.             for (i = 0; i < linecount && i < height; i++)
  69.                 atputsha(top + 1 + i, left + 1, lines[i+cur_top]);
  70.             break;
  71.  
  72.         case PAGEUP:
  73.             if (cur_top == 0)
  74.                 break;
  75.             cur_top -= height;
  76.             if (cur_top < 0)
  77.                 cur_top = 0;
  78.             clrblk(top + 1, left + 1, bottom - 1, right - 1);
  79.             for (i = 0; i < linecount && i < height; i++)
  80.                 atputsha(top + 1 + i, left + 1, lines[i+cur_top]);
  81.             break;
  82.  
  83.         case PAGEDN:
  84.             if (cur_top == linecount - height)
  85.                 break;
  86.             cur_top += height;
  87.             if (cur_top > (linecount - height))
  88.                 cur_top = linecount - height;
  89.             clrblk(top + 1, left + 1, bottom - 1, right - 1);
  90.             for (i = 0; i < linecount && i < height; i++)
  91.                 atputsha(top + 1 + i, left + 1, lines[i+cur_top]);
  92.             break;
  93.  
  94.         case DOWN:    
  95.             if (cur_top + height < linecount) {
  96.                 ++cur_top;
  97.                 scroll(1, top + 1, left + 1, bottom - 1, right - 1);
  98.                 atputsha(bottom - 1, left + 1, lines[cur_top+height-1]);
  99.             }
  100.             break;
  101.         case UP:    
  102.             if (cur_top > 0) {
  103.                 --cur_top;
  104.                 scroll(-1, top + 1, left + 1, bottom - 1, right - 1);
  105.                 atputsha(top + 1, left + 1, lines[cur_top]);
  106.             }
  107.             break;
  108.         case ESC:    
  109.             clrblk(top, left, bottom, right);
  110.             csrpop();
  111.             for (i = 0; i < linecount; i++)
  112.                 free(lines[i]);
  113.             free(lines);
  114.             return 0;
  115.             break;
  116.         default:    
  117.             break;
  118.         }
  119.     }
  120. }
  121.  
  122.  
  123. fillarray()
  124. {
  125.     int    length, i, j;
  126.     char    *leftcp, *rightcp;
  127.  
  128.     lines[linecount] = calloc(width + 1, sizeof(char));
  129.     while (!feof(ptr)) {
  130.         if (!fgets(line, sizeof(line), ptr))
  131.             break;
  132.         leftcp = rightcp = &line[0];
  133.         *strchr(line, '\n') = '\0';
  134.         if (!strlen(line)) {
  135.             linecount += 2;
  136.             lines[linecount-1] = calloc(1, 1);
  137.             lines[linecount] = calloc(width + 1, sizeof(char));
  138.             continue;
  139.         }
  140.  
  141.         /* both ptrs at first letter */
  142.  
  143.         while (*rightcp) {
  144.             while (!(isspace(*rightcp)) && (*rightcp != '\0'))
  145.                 ++rightcp;
  146.             length = rightcp - leftcp;
  147.             if (strlen(lines[linecount]) + length < width) {
  148.                 strncat(lines[linecount], leftcp, length);
  149.                 strcat(lines[linecount], " ");
  150.             } else {
  151.                 lines[++linecount] = calloc(width + 1, sizeof(char));
  152.                 strncat(lines[linecount], leftcp, length);
  153.                 strcat(lines[linecount], " ");
  154.             }
  155.             leftcp = rightcp;
  156.             while (isspace(*leftcp) && *leftcp != '\0')
  157.                 ++leftcp, ++rightcp;
  158.         }
  159.     }
  160.     ++linecount;
  161. }
  162.  
  163.  
  164. center(row, string)
  165. int    row;
  166. char    *string;
  167. {
  168.     atputsha(row, 40 - (strlen(string) / 2), string);
  169. }
  170.  
  171.  
  172. 
  173.